package cn.jw.framework.test.proxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * TODO
 *
 * @author wds_prc
 * @since 2024/7/5 22:02
 */
public class ProxyMain {

    public void mainTest(String[] args) {
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setInterfaces(IUserService.class);
        // 已知要被代理的对象的前提，然后利用方法拦截器来进行执行
        proxyFactory.addAdvice(new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                Object[] arguments = invocation.getArguments();
                for (Object obj : arguments) {
                    System.out.println(obj.toString());
                }
                RequestMapping requestMapping = invocation.getMethod().getAnnotation(RequestMapping.class);
                System.out.println(requestMapping.value()[0]);
                return null;
            }
        });
        IUserService userService = (IUserService)proxyFactory.getProxy();

        userService.addUser("成功了");
    }
}
